package ssm_zxw.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import ssm_zxw.pojo.Role;
import ssm_zxw.pojo.User;
import ssm_zxw.service.roleService;
import ssm_zxw.service.userService;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user")
public class userController {
    @Autowired
    private userService service;
    @Autowired
    private roleService roleService;

    @RequestMapping("/findALl")
    public ModelAndView findAll() {
        List<User> all = service.findAll();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("userList", all);
        modelAndView.setViewName("user-list");
        return modelAndView;
    }

    @RequestMapping("/saveUI")
    public ModelAndView saveUI() {
        List<Role> list = roleService.findAll();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("roleListUI", list);
        modelAndView.setViewName("user-add");
        return modelAndView;
    }

    @RequestMapping("/delete/{id}")
    public String deleteUser(@PathVariable(value = "id") int id) {//@PathVariable 接收请求路径占位符的值
        service.deleteUser_role(id);
        return "redirect:/user/findALl";
    }
    @RequestMapping("/login")
    public String login(String username, String password, HttpSession session) {
        User user = service.finUserByPassword(username, password);
        if (user != null) {
            session.setAttribute("user", user);
            return "redirect:/pages/main.jsp";
        }
        return "redirect:/user/login";
    }
}
